<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 创客管理
 */
namespace app\allwin\controller;
use app\allwin\model\VipCard;
use app\allwin\model\Coupon;

class Vip extends Common{

    public $mini_program   = []; 

    public function initialize() {
        parent::initialize();
        $this->assign('pathMaps',[['name'=>'会员设置','url'=>url("vip/index")]]);
    }

    /**
     * 列表
     */
    public function index(){
        $condition = [];
        $view['lists'] = VipCard::where($this->mini_program)->where($condition)->order('sort desc,id desc')->paginate(20);
        return view()->assign($view);
    }

    /**
     * 选择优惠券
     */
    public function coupon(int $id){
        $condition = [];
        $vip = VipCard::where(['id' => $id])->where($condition)->field('coupon_ids')->find();
        if(empty($vip)){
            $this->error('没有找到对应会员类型');
        }
        $view['lists']  = Coupon::where($this->mini_program)->whereIn('id',$vip->coupon_ids)->order('sort desc,id desc')->paginate(20,false,['query' => ['id' => $id]]);
        $view['vip_id'] = $id;
        return view()->assign($view);
    }

    /**
     * 弹出选择优惠券
     */
    public function winCoupon(int $vip_id){
        if(request()->isAjax()){
            $ids = $this->request->param('ids/s');
            if(empty($ids)){
                return json(['code'=>0,'msg'=>'请选择要关联的优惠券']);
            }
            $result = VipCard::editCoupon($vip_id,ids($ids,true));
            if($result){
                return json(['code'=>302,'msg'=>'关联优惠券成功','data' =>[]]);
            }else{
                return json(['code'=>0,'msg'=>'关联优惠券失败']);
            }
        }else{
            $coupon      = VipCard::where($this->mini_program)->where(['id' => $vip_id])->field('coupon_ids')->find();
            $coupon_ids  = [];
            if($coupon){
                $coupon_ids = explode(',',$coupon->coupon_ids);
            }
            $keyword = $this->request->param('keyword');
            if(!empty($keyword)){
                $sql = Coupon::where($this->mini_program)->whereLike('name','%'.$keyword.'%'); 
            }else{
                $sql = Coupon::where($this->mini_program);
            }
            $condition = [];
            $view['lists']   = $sql->whereNotIn('id',$coupon_ids)->where($condition)->order('is_platform desc,size desc,id desc')->paginate(20,false,['query' => ['vip_id' => $vip_id,'keyword'=>$keyword]]); 
            $view['vip_id']  = $vip_id;
            $view['keyword'] = $keyword;
            return view()->assign($view);
        }
    }

    /**
     * 添加
     */
    public function add(){
        if(request()->isAjax()){
            $data = [
                'member_miniapp_id' => $this->member_miniapp_id,
                'name'              => $this->request->param('name/s'),
                'price'             => $this->request->param('price/f'),
                'coupon_num'        => $this->request->param('coupon_num/d'),
                'tips'              => $this->request->param('tips/s'),
                'store_workers'     => $this->request->param('store_workers/f'),
                'store'             => $this->request->param('store/f'),
                'company_workers'   => $this->request->param('company_workers/f'),
                'company'           => $this->request->param('company/f'),
                'stock'             => $this->request->param('stock/f'),
                'group'             => $this->request->param('group/f'),
                'rule'              => $this->request->param('rule'),
                'is_tax'            => $this->request->param('is_tax/d'),
                'nominate'          => $this->request->param('nominate/d'),
            ];
            $validate = $this->validate($data,'Vipcard.edit');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $data['is_up'] = 1;
            $result =  VipCard::edit($data);
            if($result){
                return json(['code'=>200,'url'=>url('vip/index'),'msg'=>'操作成功']);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
            }
        }else{
            return view();
        }
    }

    //编辑
    public function edit(){
        if(request()->isAjax()){                
            $data = [
                'name'            => $this->request->param('name/s'),
                'price'           => $this->request->param('price/f'),
                'coupon_num'      => $this->request->param('coupon_num/d'),
                'tips'            => $this->request->param('tips/s'),
                'store_workers'   => $this->request->param('store_workers/f'),
                'store'           => $this->request->param('store/f'),
                'company_workers' => $this->request->param('company_workers/f'),
                'company'         => $this->request->param('company/f'),
                'stock'           => $this->request->param('stock/f'),
                'group'           => $this->request->param('group/f'),
                'rule'            => $this->request->param('rule'),
                'is_tax'          => $this->request->param('is_tax/d'),
                'nominate'        => $this->request->param('nominate/d'),
            ];
            $validate = $this->validate($data,'Vipcard.edit');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $id     = $this->request->param('id/d');
            $result = VipCard::edit($data,$id);
            if($result){
                return json(['code'=>200,'url'=>url('vip/index'),'msg'=>'操作成功']);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
            }
        }else{
            $view['info'] = VipCard::get(input('get.id/d'));
            return view()->assign($view);
        }
    }

    /**
     * 排序
     */
    public function sort(){
        if(request()->isAjax()){
            $data = [
                'sort' => $this->request->param('sort/d'),
                'id'   => $this->request->param('id/d'),
            ];
            $validate = $this->validate($data,'Cate.sort');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $result = VipCard::where(['id' => $data['id']])->update(['sort'=>$data['sort']]);
            if($result){
                return json(['code'=>200,'msg'=>'操作成功']);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
            }
        }
    }

    /**
     * 置顶/取消
     * @param integer $id 用户ID
     */
    public function isTop(int $id){
        VipCard::where(['member_miniapp_id' => $this->member_miniapp_id])->update(['is_up' => 0]);
        $result = VipCard::where(['member_miniapp_id' => $this->member_miniapp_id,'id' =>$id])->update(['is_up' => 1]);
        if(!$result){
            return json(['code'=>0,'message'=>'操作失败']);
        }else{
            return json(['code'=>200,'message'=>'操作成功']);
        }
    } 

    //删除
    public function delete(int $id){
        $result = VipCard::where($this->mini_program)->where(['id'=>$id])->delete();
        if($result){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>403,'msg'=>'删除失败']);
        }
    }

   //删除
   public function delCoupon(int $vip_id,$coupon_id){
        $info   = VipCard::where(['id' => $vip_id])->find();
        $result = false;
        if($info){
            $coupon_ids =  ids(array_values_unset($coupon_id,explode(',',$info->coupon_ids)));
            $result = VipCard::where($this->mini_program)->where(['id' => $vip_id])->update(['coupon_ids' => $coupon_ids]);
        }
        if($result){
            return json(['code' =>200,'msg'=>'操作成功']);
        }else{
            return json(['code' => 0,'msg'=>'操作失败']);
        }
    }
}